package com.zhanghe.mall.web.controller;

import com.zhanghe.mall.pojo.ClassificationAttribute;
import com.zhanghe.mall.pojo.Commodity;
import com.zhanghe.mall.service.ClassificationAttributeService;
import com.zhanghe.mall.service.dto.AddClassficationDTO;
import com.zhanghe.mall.service.vo.ClassificationAttributeVO;
import com.zhanghe.mall.service.vo.CommodityClassificationTreeVO;
import com.zhanghe.mall.service.CommodityClassificationService;
import com.zhanghe.mall.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CommodityController
 *
 * @author Clevo
 * @date 2018/7/16
 */
@RestController
public class CommodityController {

    @Autowired
    private CommodityClassificationService commodityClassificationService;


    /**
     * 获取首页所有一级分类
     * @return
     */
    @GetMapping("/classification/root")
    public String getRootClassification(){
        List<CommodityClassificationTreeVO> resultList = CommodityClassificationTreeVO.transformList(commodityClassificationService.getRootClassfication());
        R r = new R(resultList);
        return r.toString();
    }

    /**
     * 获取所有的商品分类树
     * @return
     */
    @GetMapping("/classifications")
    public String getCommodityClassifications(){
        List<CommodityClassificationTreeVO> result = commodityClassificationService.getClassificationTree();
        return new R(result).toString();
    }

    /**
     * 添加分类
     * @param dto
     * @param parentId
     * @return
     */
    @PostMapping("/classifications")
    public String addCommodityClassification(AddClassficationDTO dto,Long parentId){
        commodityClassificationService.addClassificationTree(dto,parentId);
        return new R(1).toString();
    }

    /**
     *  删除商品分类
     * @param id
     * @return
     */
    @DeleteMapping("/classifications/{id}")
    public String deleteCommodityClassification(@PathVariable Long id){
        commodityClassificationService.deleteClassification(id);
        return new R(1).toString();
    }

    /**
     * 添加商品
     * @param commodityName
     * @param sketch
     * @param brandId
     * @return
     */
    public String addCommodity(String commodityName,String sketch,Long brandId){
        Commodity commodity = new Commodity();
        return new R(1).toString();
    }

    /**
     * 获取添加商品需要的信息
     * @return
     */
    public String addCommidityInfo(){
        //获取一级分类
        List<CommodityClassificationTreeVO> rootClassification = CommodityClassificationTreeVO.transformList(commodityClassificationService.getRootClassfication());
        return new R(rootClassification).toString();
    }

}
